<?php
	//Initial date: 2009-05-13
	//Last change: 2009-05-30 by Kamil Jaskiewicz
	include("classes/DBTable.class.php");
	include("classes/companies.class.php"); 
	include("classes/departments.class.php");

	$module_name="Companies";
	$action=$_REQUEST['action'];
	$companies=new companies();

	if ($_REQUEST['submit'])
	{
		$id		= $_REQUEST['id'];
		$c_name = $_REQUEST['company_name'];
		$c_type	= $_REQUEST['type'];

		if ($action == 'edit')
		{
			$companies->DB->query("UPDATE companies SET company_name='$c_name', company_type='$c_type'
				WHERE company_id='$id'");

		} 
		else if ($action == 'add')
		{
			$q = "INSERT INTO companies VALUES ('', '$c_name', '$c_type')";
			$companies->DB->query($q);			
			$id = $companies->DB->LastID;
		}
		header("location:companies.php?action=view&id=$id");
		exit();
	}

	if ($action == 'delete')
	{
		$id = $_REQUEST['id'];
		if($id)
		{
			$companies->delete($id);
			header("location:companies.php");
		}
	}

	include("header.php");
	
	//action not defined, display all companies
	if (!$action)
	{
		$companies->selectAll();

		print("<table id='grid'>");
		print("<tr><td><b>Companies</b></td><td><b>Company&nbsp;type</b></td><td colspan='3'></td></tr>\n");
		while($companies->getNext())
		{
			print("<tr>");
			print("<td><a href='companies.php?action=view&id=$companies->company_id'>".str_replace(" ", "&nbsp;", $companies->company_name)."</a></td>");
			print("<td>".str_replace(" ", "&nbsp;", $companies->type)."</td>");
			print("<td><a href='companies.php?action=edit&id=$companies->company_id'>Edit</a></td>");
			print("<td><a href='companies.php?action=delete&id=$companies->company_id' onclick='return confirmation(\"Are you sure you want to delete this company (all projects and departments assigned to this company also be deleted)?\");' >Delete</a></td>");
			print("<td style='width:80%'>&nbsp;</td>");
			print("</tr>\n\n");
		}
		print("<tr><td colspan='5' style='text-align:right'><a href='companies.php?action=add'>Add new company</a></td></tr>");
		print("</table>");
	}

	//display details of the company
	if ($action == 'view')
	{
		$id=$_REQUEST['id'];
		$companies->findID($id);

		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td>$companies->company_name</td></tr>\n");
		print("<tr><td><b>Type</b></td><td>$companies->type</td></tr>\n");
		print("<tr><td colspan='2'><a href='companies.php?action=edit&id=$companies->company_id'>Edit</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='companies.php?action=delete&id=$companies->company_id' onclick='return confirmation(\"Are you sure you want to delete this company (all projects and departments assigned to this company also be deleted)?\");' >Delete</a></td></tr>\n");
		print("</table>");

		$dept = new departments();
		$dept->selectForCompany($id);
		print("<h3>Company departments</h3>");
		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td></td>");
		while($dept->getNext())
		{
			print("<tr><td>$dept->dept_name</td>");
			print("<td><a href='departments.php?action=edit&id=$dept->dept_id'>Edit</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='departments.php?action=delete&id=$dept->dept_id&company_id=$id' onclick='return confirmation(\"Are you sure you want to delete this department (all contacts and tasks assigned to this department also be deleted)?\");' >Delete</a></td></tr>\n");
		}
		print("<tr><td colspan='2' style='text-align:right'><a href='departments.php?action=add&company_id=$id'>Add new department</a></td></tr>");
		print("</table>");
	}

	if ($action == 'edit' || $action == 'add')
	{
		$id=$_REQUEST['id'];
		if ($id)
			$companies->findID($id);
		$company=new companies();
		print("<form action='companies.php' method='post'>");
		if ($action == 'edit')
		{
			print("<input type='hidden' name='action' value='edit' />");
			print("<input type='hidden' name='id' value='$id' />");
		}
		else if ($action == 'add')
		{
			print("<input type='hidden' name='action' value='add' />");
		}
		print("<input type='hidden' name='submit' value='true' />");
		print("<table id='grid'>");
		print("<tr><td><b>Name</b></td><td><input type='text' value='$companies->company_name' name='company_name'/></td></tr>\n");
		print("<tr><td><b>Type</b></td><td><select name='type'>\n");
		$company->getPossibleTypeValues();
		while($company->getNext())
		{
			if ($company->dict_id != $companies->company_type)
				print("<option value='$company->dict_id'>$company->name</option>\n");
			else
				print("<option selected value='$company->dict_id'>$company->name</option>\n");
		}
		print ("</select></td></tr>\n");
		print("<tr><td colspan='2'><input type='submit' value='Submit' />&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='Cancel' onclick='history.back();' /></td></tr>\n");
		print("</table>");
		print("</form>");
	}
	
	include("footer.php");
?>